package club.zhilin.guli.service.trade.controller.portal;

import club.zhilin.guli.common.base.domain.JwtInfo;
import club.zhilin.guli.common.base.result.CommonResult;
import club.zhilin.guli.common.base.result.CommonResultCodeEnum;
import club.zhilin.guli.common.base.util.JwtUtil;
import club.zhilin.guli.service.trade.domain.Order;
import club.zhilin.guli.service.trade.service.IOrderService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhilin
 * @since 2022/3/1 17:01
 */
@RestController
@RequestMapping("/portal/trade/order")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class PortalOrderController {

    private final IOrderService iOrderService;

    @ApiOperation("新增订单")
    @PostMapping("/auth/order/{courseId}")
    public CommonResult<String> save(@PathVariable String courseId, @ApiIgnore HttpServletRequest request) {
        CommonResult<JwtInfo> jwtInfoCommonResult = JwtUtil.requestGetMember(request);
        if (jwtInfoCommonResult.getData() == null) {
            return CommonResult.failed("请先进行登录");
        }
        return iOrderService.saveOrder(courseId, jwtInfoCommonResult.getData().getId());
    }

    @ApiOperation("获取订单")
    @GetMapping("/auth/order/{id}")
    public CommonResult<Order> selectOrder(@PathVariable String id, @ApiIgnore HttpServletRequest request) {
        CommonResult<JwtInfo> jwtInfoCommonResult = JwtUtil.requestGetMember(request);
        if (jwtInfoCommonResult.getData() == null) {
            return CommonResult.failed("请先进行登录");
        }
        Order order = iOrderService.getById(id);
        return order != null ? CommonResult.success(order) : CommonResult.failed("未查询到指定记录");
    }

    @ApiOperation("判断课程是否购买")
    @GetMapping("/auth/order/isPaid/{courseId}")
    public CommonResult<Map<String, Boolean>> isPaid(@PathVariable String courseId, @ApiIgnore HttpServletRequest request) {
        CommonResult<JwtInfo> jwtInfoCommonResult = JwtUtil.requestGetMember(request);
        if (jwtInfoCommonResult.getData() == null) {
            return CommonResult.failed("请先进行登录");
        }
        Boolean isPaid = iOrderService.selectUserIsPaid(courseId, jwtInfoCommonResult.getData().getId());
        Map<String, Boolean> result = new HashMap<>(1);
        result.put("isPaid", isPaid);
        return CommonResult.success(result);
    }

    @ApiOperation("获取当前用户的订单列表")
    @GetMapping("/auth/order/list")
    public CommonResult<List<Order>> list(HttpServletRequest request) {
        CommonResult<JwtInfo> jwtInfoCommonResult = JwtUtil.requestGetMember(request);
        if (jwtInfoCommonResult.getData() == null) {
            return CommonResult.failed("请先进行登录");
        }
        List<Order> list = iOrderService.list(new LambdaQueryWrapper<Order>().eq(Order::getMemberId, jwtInfoCommonResult.getData().getId()).eq(Order::getIsDeleted, false).orderByDesc(Order::getGmtCreate));
        return CommonResult.success(list);
    }

    @ApiOperation("删除订单")
    @DeleteMapping("/auth/order/{orderId}")
    public CommonResult<String> removeOrder(@PathVariable String orderId, HttpServletRequest request) {
        CommonResult<JwtInfo> jwtInfoCommonResult = JwtUtil.requestGetMember(request);
        if (jwtInfoCommonResult.getData() == null) {
            return CommonResult.failed("请先进行登录");
        }
        boolean result = iOrderService.removeOrder(orderId, jwtInfoCommonResult.getData().getId());
        return result ? CommonResult.success("删除成功") : CommonResult.failed("删除失败");
    }

    @ApiOperation("查询订单状态")
    @GetMapping("/auth/select/order/status/{orderNo}")
    public CommonResult<CommonResultCodeEnum> selectPaidStatus(@PathVariable String orderNo, HttpServletRequest request) {
        CommonResult<JwtInfo> jwtInfoCommonResult = JwtUtil.requestGetMember(request);
        if (jwtInfoCommonResult.getData() == null) {
            return CommonResult.failed("请先进行登录");
        }
        boolean result = iOrderService.selectPaidStatus(orderNo, jwtInfoCommonResult.getData().getId());
        return result ? CommonResult.success("支付成功") : CommonResult.success(CommonResultCodeEnum.PAY_RUN);
    }
}
